<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-dialog :title="title" :visible.sync="dialogFormVisible">
                    <el-form :model="addForm" :rules="rules" ref="addForm">
                        <el-form-item label="发布时间" :label-width="formLabelWidth">
                            <el-date-picker v-model="addForm.publicAt" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                            <el-input type="text" v-model="addForm.title" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="副标题" :label-width="formLabelWidth">
                            <el-input type="text" v-model="addForm.subTitle" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="内容" :label-width="formLabelWidth">
                            <!-- <el-input type="textarea" v-model="addForm.content" auto-complete="off"></el-input> -->
                            <editor @input="htmlOutput" :htmlContent="addForm.content"></editor>
                        </el-form-item>
                        <el-form-item label="要闻分类" :label-width="formLabelWidth">
                            <el-select v-model="addForm.allianceId" placeholder="请选择">
                                <el-option
                                    v-for="item in allianceList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="来源" :label-width="formLabelWidth">
                            <el-input type="text" v-model="addForm.origin" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="作者" :label-width="formLabelWidth">
                            <el-input type="text" v-model="addForm.author" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="add">确 定</el-button>
                    </div>
                </el-dialog>
                <el-input v-model="select_word" placeholder="输入文章标题" class="search"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="primary" @click="addNews">新增联盟活动</el-button>
            </div>
            <div>
                <el-table :data="tableData" border>
                    <el-table-column label="发布时间" prop="publicAt" :formatter="timeFomatter"></el-table-column>
                    <el-table-column label="标题" prop="title"></el-table-column>
                    <el-table-column label="副标题" prop="subTitle"></el-table-column>
                    <el-table-column label="要闻类型" prop="fTypeId" :formatter="typeFomatter"></el-table-column>
                    <el-table-column label="内容" prop="content" show-overflow-tooltip></el-table-column>
                    <el-table-column label="来源" prop="origin"></el-table-column>
                    <el-table-column label="作者" prop="author"></el-table-column>
                    <el-table-column prop="state" label="状态" show-overflow-tooltip empty-text :formatter="stateFormatter">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="put(scope.row)">编辑</el-button>
                            <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
                            <el-switch
                                v-model="scope.row.state === 1 ? true : false" 
                                active-color="#13ce66"
                                @change="switchChange(scope.row)"
                            >
                            </el-switch>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" 
                :current-page="page.number" 
                layout="total,  prev, pager, next, jumper" 
                :total="page.total">
                </el-pagination>
            </div>

        </div>
    </div>
</template>
<script>
import Editor from './../../components/common/Editor';
import { allianceActivity, alliance } from '../../server/getDate.js'
import { mapState, mapActions } from 'vuex'
import { dateFormatter, filterObj } from '../../utils/local.js'
const { activityListApi, addApi, updateApi, deleteApi, disableApi } = allianceActivity
const { listApi } = alliance
export default {
    data() {
        return {
            formLabelWidth: '120px',
            select_word: '',
            dialogFormVisible: false,
            addForm: {},
            title: '',
            rules: {
                title: [{ required: true, message: '请输入标题', trigger: 'blur' }]        
            },
        }
    },
    created() {
        this.getAlliance()
        this.getList(1)
    },
    components: {
        Editor
    },
    computed: mapState({
        tableData: state => state.Org.activityList,
        page: state => state.Org.activityPage,
        allianceList: state => state.Org.allianceList
    }),
    methods: {
        ...mapActions(['saveAlliance', 'saveActivity']),
        handleCurrentChange(val) {
            this.getList(val)
        },
        htmlOutput(html) {
            this.addForm.content = html
        },
        getAlliance() {
            listApi({ page: 1, pageSize: 100000 }).then(res => {
                if (res.code === '000000') {
                    this.saveAlliance(res)
                } else {
                    this.$message.error(res.message)
                }
            }, () => {
                this.$message.error('网络错误')
            })
        },
        getList(number) {
            const params = {
                page: number,
                pageSize: 10
            }
            if (this.select_word !== '') {
                params.searchKey = this.select_word
            }
            activityListApi({ ...params }).then(res => {
                if (res.code === '000000') {
                    this.saveActivity({
                        ...res,
                        number
                    })
                } else {
                    this.$message.error(res.message)
                }
            }, () => {
                this.$message.error('网络错误')
            })
        },
        search() {
            this.getList(this.page.number)
        },
        del(row) {
            this.$confirm('此操作将永久删除该名称, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteApi({ ids: row.id }).then(res => {
                    if (res.code === '000000') {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList(this.page.number)
                    } else {
                        this.$message.error(res.message)
                    }   
                }, err => this.$message.error(err.message))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        put(row) {
            this.title = '编辑联盟活动'
            this.dialogFormVisible = true
            this.addForm = filterObj(row)
            if (dateFormatter(row.publicAt)) {
                this.addForm.publicAt = dateFormatter(row.publicAt)
            }
        },
        addNews() {
            this.dialogFormVisible = true
            this.addForm = {}
            this.title = '新增联盟活动'
        },
        add() {
            this.$refs['addForm'].validate(valid => {
                if (valid) {
                    this.dialogFormVisible = false
                    if (this.addForm.id) {
                        updateApi({ ...this.addForm }).then(res => {
                            if (res.code === '000000') {
                                this.$message.success('修改成功')
                                this.getList(this.page.number)
                            } else {
                                this.$message.error(res.message)
                            }
                        }, () => {
                            this.$message.error('网络错误')
                        })
                    } else {
                        addApi({ ...this.addForm }).then(res => {
                            if (res.code === '000000') {
                                this.$message.success('添加成功')
                                this.getList(this.page.number)
                            } else {
                                this.$message.error(res.message)
                            }
                        }, () => {
                            this.$message.error('网络错误')
                        })
                    }
                }
            })
        },
        timeFomatter(row) {
            return dateFormatter(row.publicAt)
        },
        stateFormatter(row) {
          return row.state === 1 ? '启用' : '禁用'
        },
        typeFomatter(row) {
            if (this.allianceList.length === 0) {
                return '无'
            } else {
                let state = ''
                this.allianceList.some(item => {
                    if (item.id === row.allianceId) {
                        state = item.name
                    }
                })
                return state
            }
        },
        switchChange(row) {
            disableApi({ 
                ids: row.id, 
                state: row.state ? '0' : '1' 
            }).then(res => {
                if (res.code === '000000') {
                    this.$message.success('修改成功')
                    this.getList(this.page.number)
                } else {
                    this.$message.error(res.message)
                }
            }, () => {
                this.$message.error('网络错误')
            })
        }
    }
};
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>
